package com.sx.micowell.framework.adapter.binding.adapter.base;

import android.content.Context;

import androidx.annotation.LayoutRes;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.sx.micowell.framework.tools.DiffUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 多类型适配器
 * @Author 梁浩文
 * @Date 2021/6/11 9:56
 */
public abstract class MultiTypeAdapter extends SimpleDataBindingAdapter<MultiTypeBean, ViewDataBinding> {
    public Map<Integer, Integer> layoutMap = new HashMap<>();

    public MultiTypeAdapter(Context context) {
        super(context, 0, DiffUtils.getInstance().getMultiTypeBeanItemCallback());
    }

    public void addTypeView(int Type, @LayoutRes int layoutId) {
        layoutMap.put(Type, layoutId);
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return viewType;
    }

    @Override
    protected void onBindItem(ViewDataBinding binding, MultiTypeBean item, RecyclerView.ViewHolder holder) {
        onBindItem(binding, item.TYPE, item, holder, getCurrentList().indexOf(item));
    }

    public abstract void onBindItem(ViewDataBinding binding, int type, MultiTypeBean item, RecyclerView.ViewHolder holder, int position);


    @Override
    public int getItemViewType(int position) {
        return layoutMap.get(getItem(position).TYPE);
    }
}
